package com.holyant.goods.controller;

import com.ruoyi.common.core.domain.ApiRes;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.holyant.goods.dto.GdsBrandDto;
import com.holyant.goods.dto.GdsBrandImportDto;
import com.holyant.goods.dto.query.GdsBrandQueryDto;
import com.holyant.goods.service.IGdsBrandService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.holyant.goods.constants.CommonConstatns.BRAND_EXPORT_MAX_SIZE;

/**
 * 商品品牌Controller
 * 
 * @author fkmalls
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/goods/brand")
public class GdsBrandController extends BaseController {
    @Autowired
    private IGdsBrandService gdsBrandService;

    /**
     * 查询商品品牌列表
     */
//    @PreAuthorize(hasPermi = "goods:brand:list")
    @PostMapping("/list")
    public ApiRes<List<GdsBrandDto>> list(@RequestBody GdsBrandQueryDto gdsBrandQueryDto) {
        this.startPage(gdsBrandQueryDto);
        List<GdsBrandDto> list = gdsBrandService.selectGdsBrandList(gdsBrandQueryDto);
        return ApiRes.ok(list,"查询成功",new PageInfo(list).getTotal());
    }

    /**
     * 导出商品品牌列表
     */
    //@PreAuthorize(hasPermi = "goods:brand:export")
    @Log(title = "商品品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GdsBrandQueryDto gdsBrandQueryDto) throws IOException {
        List<GdsBrandDto> list = gdsBrandService.selectGdsBrandList(gdsBrandQueryDto);
        AssertUtils.isTrue(BRAND_EXPORT_MAX_SIZE.compareTo(list.size())>0,"数据过多，请更改查询提交，一次最多导出"+BRAND_EXPORT_MAX_SIZE+"条");
        ExcelUtil<GdsBrandDto> util = new ExcelUtil<GdsBrandDto>(GdsBrandDto.class);
        util.exportExcel(response, list, "brand");
    }

    /**
     * 获取商品品牌详细信息
     */
//    @PreAuthorize(hasPermi = "goods:brand:query")
    @GetMapping(value = "/{id}")
    @Cacheable(value = "BRAND", key = "#root.args[0]")
    public ApiRes<GdsBrandDto> getInfo(@PathVariable("id") Long id) {
        return ApiRes.ok(gdsBrandService.selectGdsBrandById(id));
    }

    /**
     * 新增商品品牌
     */
    @PreAuthorize(hasPermi = "goods:brand:add")
    @Log(title = "商品品牌", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public ApiRes<?> add(@RequestBody GdsBrandDto gdsBrandDto) {
        return ApiRes.ok(gdsBrandService.insertGdsBrand(gdsBrandDto));
    }

    /**
     * 修改商品品牌
     */
    @PreAuthorize(hasPermi = "goods:brand:edit")
    @Log(title = "商品品牌", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public ApiRes<?> update(@RequestBody GdsBrandDto gdsBrandDto) {
        return toApiRes(gdsBrandService.updateGdsBrand(gdsBrandDto));
    }

    /**
     * 删除商品品牌
     */
    @PreAuthorize(hasPermi = "goods:brand:remove")
    @Log(title = "商品品牌", businessType = BusinessType.DELETE)
	@PostMapping("/delete/{ids}")
    public ApiRes<?> remove(@PathVariable Long[] ids) {
        return toApiRes(gdsBrandService.deleteGdsBrandByIds(ids));
    }

    /**
     * 停用商品品牌
     */
    @PreAuthorize(hasPermi = "goods:brand:close")
    @Log(title = "商品品牌", businessType = BusinessType.OTHER)
    @PostMapping("/close/{id}")
    public ApiRes<?> close(@PathVariable Long id) {
        return toApiRes(gdsBrandService.close(id));
    }
    
    @PostMapping("/updateBrandCode")
    public ApiRes<?> updateBrandCode(){
        return toApiRes(gdsBrandService.updateBrandCode());
    }

    /**
     * 初始化临时接口
     * @return
     */
    @PostMapping("/initBrandUniformCode")
    public ApiRes<?> initBrandUniformCode(){
        return toApiRes(gdsBrandService.initBrandUniformCode());
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<GdsBrandImportDto> util = new ExcelUtil<>(GdsBrandImportDto.class);
        util.exportExcel(response, Collections.singletonList(new GdsBrandImportDto()), "品牌导入模板");
    }

    @PostMapping("/importData")
    public ApiRes<Map<String,List<GdsBrandImportDto>>> importData(MultipartFile file) throws Exception {
        ExcelUtil<GdsBrandImportDto> brandImportDto =  new ExcelUtil<>(GdsBrandImportDto.class);
        List<GdsBrandImportDto> gdsBrandImportDtos = brandImportDto.importExcel(file.getInputStream()).stream()
                .filter(Objects::nonNull)
                .filter(item-> StringUtils.isNotEmpty(item.getBrandName()))
                .collect(Collectors.toList());
        return gdsBrandService.importData(gdsBrandImportDtos);
    }


    @PostMapping("/importDataUpdate")
    public ApiRes<?> importDataUpdate(@RequestBody List<GdsBrandImportDto> gdsBrandImportDtos) {
        return toApiRes(gdsBrandService.importDataUpdate(gdsBrandImportDtos));
    }

}
